import 'dart:io' show Platform;
import 'package:casdon/common/widget/conform_dialog.dart';
import 'package:casdon/core/logger/log.dart';
import 'package:casdon/core/network/business_exception.dart';
import 'package:casdon/modules/me/data/repository/me_repository.dart';
import 'package:casdon/modules/me/data/repository/me_repository_impl.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:package_info_plus/package_info_plus.dart';

class AppUpdateInfoUsecase {
  final MeRepository _repository;

  const AppUpdateInfoUsecase(this._repository);

  Future<Map<String, dynamic>> execute() async {
    try {
      var app = await PackageInfo.fromPlatform();
      var platform = "android";
      if (Platform.isIOS) {
        platform = "ios";
      }
      var ret = await _repository.getAppVersionInfo(platform, app.version);

      return ret;
    } on BusinessException {
      rethrow;
    } catch (e) {
      Log.e("AppUpdateInfoUsecase 失败", e.toString(), StackTrace.current);
      throw Exception("AppUpdateInfoUsecase 失败");
    }
  }
}

final appUpdateInfoUsecaseProvider = Provider(
  (ref) => AppUpdateInfoUsecase(ref.read(meRepositoryProvider)),
);
